﻿/*
 * Copyright 2012 Rene Prost, Registrite ja Infosüsteemide Keskus 
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using Xtee.CodeDom.MetaInfo;

namespace Xtee.CodeDom.Test
{
    [TestFixture]
    public class ClassInfoTests :BaseFixture
    {
         [Test]
        public void ShouldResolveFullAnonymousNameForPropertyReferArrayOfDecimals()
        {
             var obj = GetTestComplextType("ReferArrayOfDecimals");
             var cls = LoadClass(obj);
             Write(cls.Properties[0] as ElementInfo);
             Assert.That(((ElementInfo)cls.Properties[0]).ArrayFullTypeName, Is.EqualTo("System.Collections.Generic.List<System.Decimal?>"));
             Assert.That(((ElementInfo)cls.Properties[0]).ArrayItemElementInfo.FullTypeName, Is.EqualTo("Xtee.Core.Schema.XsdDecimal"));
         }

         private static SchemaObjectClassInfo LoadClass(System.Xml.Schema.XmlSchemaComplexType obj)
         {
             var cls = SchemaObjectClassInfo.Load(obj, null,GlobalSetup.Resolver);
             return (SchemaObjectClassInfo)cls;
         }

         private void Write(ElementInfo elementInfo)
         {
             Console.WriteLine(elementInfo.ArrayFullTypeName);
             Console.WriteLine(elementInfo.ArrayItemElementInfo.FullTypeName);
         } 

         [Test]
         public void ShouldResolveFullAnonymousNameForPropertyInlineComplexTypeArray()
         {
             var obj = GetTestComplextType("InlineComplexTypeArray");
             var cls = LoadClass(obj);
             Write(cls.Properties[0] as ElementInfo);
             Assert.That(((ElementInfo)cls.Properties[0]).ArrayFullTypeName, Is.EqualTo("System.Collections.Generic.List<InlineComplexTypeArray.ManagedRegionsType>"));
             Assert.That(((ElementInfo)cls.Properties[0]).ArrayItemElementInfo.FullTypeName, Is.EqualTo("InlineComplexTypeArray.ManagedRegionsType"));
         }
         [Test]
         public void ShouldResolveFullAnonymousNameForPropertyInlineSimpleTypeArray()
         {
             var obj = GetTestComplextType("InlineSimpleTypeArray");
             var cls = LoadClass(obj);
             Write(cls.Properties[0] as ElementInfo);
             Assert.That(((ElementInfo)cls.Properties[0]).ArrayFullTypeName, Is.EqualTo("System.Collections.Generic.List<InlineSimpleTypeArray.WorkTitlesType>"));
             Assert.That(((ElementInfo)cls.Properties[0]).ArrayItemElementInfo.FullTypeName, Is.EqualTo("InlineSimpleTypeArray.WorkTitlesType"));
         }
         [Test]
         public void ShouldResolveFullAnonymousNameForPropertyReferencedComplexTypeArray()
         {
             var obj = GetTestComplextType("ReferencedComplexTypeArray");
             var cls = LoadClass(obj);
             Write(cls.Properties[0] as ElementInfo);
             Assert.That(((ElementInfo)cls.Properties[0]).ArrayFullTypeName, Is.EqualTo("System.Collections.Generic.List<global::Kis.Xroad.Eu.Employee>"));
             Assert.That(((ElementInfo)cls.Properties[0]).ArrayItemElementInfo.FullTypeName, Is.EqualTo("global::Kis.Xroad.Eu.Employee"));
         }
        [Test]
        public void ShouldResolveFullAnonymousNameForPropertyDoubleAnonymous()
        {
            var obk = "DoubleAnonymous";
            var obj =  GetTestComplextType(obk);
            var cls = LoadClass(obj);

            var anonymous = cls.GetAnonymousClasses()[0];
            var anonymous2 = anonymous.GetAnonymousClasses()[0];

            var level1 = cls.Properties[0];
            var level3 = anonymous.Properties[0];
            var level5 = anonymous2.Properties[0];

            Assert.That(((ElementInfo)level1).ArrayFullTypeName,Is.EqualTo("System.Collections.Generic.List<DoubleAnonymous.Level1Type>"));
            Assert.That(((ElementInfo)level3).ArrayFullTypeName, Is.EqualTo("System.Collections.Generic.List<DoubleAnonymous.Level1Type.Level3Type>"));
            Assert.That(((ElementInfo)level5).FullTypeName, Is.EqualTo("Xtee.Core.Schema.XsdString"));
        }


    }
}
